<template>
  <div class="risk">
    <div class="add" @click="edit()">新增记录</div>
    <eagle-page-list
      ref="eaglePageList"
      @bindData="bindData"
      :queryParams="queryParams"
      :controller="controller"
      :method="method"
      :conditions="conditions"
      :pageSize="20"
    >
      <template #list>
        <eagle-cell 
          style="margin-top: 10px"
          class="br-8"
          v-for="(item, index) in list"
          :key="index">
          <div class="warn_item">
          <div>
            <div class="warn_header">
              <div>附件名称：{{ item.PartsName }}</div>
              <!-- <van-tag
                v-if="item.ExpireDays < 0"
                class="warn_tag"
                type="primary"
                >剩{{ Math.abs(item.ExpireDays) }}天</van-tag
              >
              <van-tag v-if="item.ExpireDays > 0" class="warn_tag" type="danger"
                >逾期{{ Math.abs(item.ExpireDays) }}天</van-tag
              > -->
            </div>
            <div class="dep_sort">
              <img src="@/assets/Images/AppIcon/icon_number.png" alt="" />
              <div>编号：{{ item.PartsCode }}</div>
            </div>
            <div class="dep_sort">
              <img src="@/assets/Images/AppIcon/icon_list_user.png" alt="" />
              <div>检查人：{{ item.CreateChnName }}</div>
            </div>

            <div class="dep_sort">
              <img src="@/assets/Images/AppIcon/icon_list_date.png" alt="" />
              <div>最近检验日期：{{ item.CheckDate || "无" }}</div>
            </div>
            <div class="dep_sort">
              <img src="@/assets/Images/AppIcon/icon_list_date.png" alt="" />
              <div>下次检验日期：{{ item.NextCheckDate || "无" }}</div>
            </div>
          </div>
          <eagle-divider />
          <div class="dep_btm">
            <div class="btm_item" @click="edit(item)">
              <img src="@/assets/Images/AppIcon/icon_list_edit.png" alt="" />
              <div class="mr-20">编辑</div>
            </div>
            <div class="btm_item" @click="handleDelete(item)">
              <img src="@/assets/Images/AppIcon/icon_list_edit.png" alt="" />
              <div class="mr-10">删除</div>
            </div>
          </div>
        </div>
        </eagle-cell>
      </template>
    </eagle-page-list>
    <eagle-dialog
      v-model="show"
      :title="title"
	    :titlePosition="'center'"
      @confirm="handleConfirm()"
      show-cancel-button
	    show-confirm-button
    >
      <eagle-form style="padding: 20px">
        <eagle-input
          label="最近检验日期"
          v-model="form.CheckDate"
          name="CheckDate"
          placeholder="点击选择日期"
          @click="handleShow('CheckDate')"
          readonly
        ></eagle-input>
        <eagle-input
          label="下次检验日期"
          v-model="form.NextCheckDate"
          name="NextCheckDate"
          placeholder="点击选择日期"
          @click="handleShow('NextCheckDate')"
          readonly
        ></eagle-input>
      </eagle-form>
    </eagle-dialog>
    <eagle-calendar
      color="#1989fa"
      v-model="showCalendar"
      :min-date="new Date('2000-01-01')"
      :max-date="new Date('2040-01-01')"
      @confirm="onConfirm"
    />
  </div>
</template>

<script>
import { Dialog } from "vant";
import { formatDate, getDate } from "@/utils/getDate";
export default {
  data() {
    return {
      list: [],
      conditions: {},
      controller: "/EquEquipment",
      method:"/GetSafeFilePageData",
      queryParams: {
        equSafeFileID: this.$route.query.equSafeFileID,
      },
      showCalendar: false,
      show: false,
      value: "",
      form: {},
      title: "",
    };
  },
  created() {
  },
  methods: {
    bindData(data) {
      this.list = [];
      if (data) {
        this.list = data;
      }
    },
    edit(obj) {
      if (obj) {
        this.title = "编辑检查历史";
        this.form.CheckDate = getDate(obj.CheckDate);
        this.form.NextCheckDate = getDate(obj.NextCheckDate);
        this.form.ID = obj.ID;
        this.show = true;
      } else {
        let _this = this;
        _this.title = "新增检查历史";
        _this.$axios
        .get("/EquEquipment/InitEquSafeFileCheckEntity",{params:{safeFileId: _this.$route.query.equSafeFileID}})
        .then((res)=>{
          _this.form = res.data;
          _this.form.CheckDate = getDate(res.data.CheckDate);
          _this.form.NextCheckDate = getDate(res.data.NextCheckDate);
          _this.show = true;
        })
        .catch((err) => {
          console.log("err", err);
        });
      }
    },
    handleShow(name) {
      this.showCalendar = true;
      this.selectKey = name;
    },
    onConfirm(date) {
      const dateTime = formatDate(date);
      this.form[this.selectKey] = getDate(dateTime);
      this.showCalendar = false;
    },
    handleDelete(item) {
      Dialog.confirm({
        title: "询问",
        message: "确定要删除该条附件记录吗?",
      })
        .then(() => {
          // on confirm
          let _this = this;
          _this.$axios
          .get("/EquEquipment/DeleteCheckEquipmenSafeFile",{params:{id: item.ID}})
          .then((res)=>{
            _this.$toast.success("删除成功！");
            _this.$refs.eaglePageList.confirm();
          })
        })
        .catch(() => {
          // on cancel
        });
    },
    handleConfirm() {
      //this.form.equipmentCode = this.$route.query.equipmentCode
      let _this = this;
      _this.$axios
      .post("/EquEquipment/EditCheckEquipmenSafeFile",this.form)
      .then((res)=>{
        _this.$toast.success("编辑成功！");
        _this.$refs.eaglePageList.confirm();
      })
    },
    getFile() {
      this.$router.push({
        path: "/Equipment/SafetyFile",
        query: { equipmentCode: equipmentCode },
      });
    },
    // 详情
    goDetail(ChemCode) {
      this.$router.push({
        path: "/Chemical/Detail",
        query: { ChemCode: ChemCode },
      });
    },
  },
};
</script>

<style lang="less" scoped>
.risk {
  height: 100vh;
  background-color: #e8f1f8;
  .info_search {
    padding: 10px;
    border-radius: 5px !important;
    .van-field__control {
      background-color: #ffffff !important;
    }
  }
}
.van-cell {
  padding: 0 0;
}
.all_dep {
  background-color: #ffffff;
}
.warn_item {
  /*margin-bottom: 10px;*/
  padding: 10px 0 10px 10px;
  border-radius: 5px;
  position: relative;
}
.warn_header {
  display: flex;
  justify-content: space-between;
  position: relative;
  margin-bottom: 10px;
  .warn_tag {
    position: absolute;
    right: -4px;
    top: 0;
    border-radius: 26.64rem 0 0 26.64rem;
  }
}
.dep_sort {
  display: flex;
  align-items: center;
  margin-bottom: 5px;
  img {
    height: 15px;
    width: 15px;
    margin-right: 5px;
  }
}
.dep_btm {
  display: flex;
  justify-content: flex-end;
  .btm_item {
    display: flex;
    align-items: center;
    img {
      height: 15px;
      width: 15px;
      margin-right: 5px;
    }
  }
}
</style>
